package cn.cxion.chat.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.lqr.optionitemview.OptionItemView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.cxion.chat.R;
import cn.cxion.chat.app.activity.ConstantActivity;
import cn.cxion.chat.app.bean.AppointmentInfo;
import cn.cxion.chat.app.bean.CheckContractBean;
import cn.cxion.chat.app.bean.SecBean;
import cn.cxion.chat.app.constant.Constant;
import cn.cxion.chat.app.util.LocalDisplay;
import cn.cxion.chat.app.util.LogUtil;
import cn.cxion.chat.app.util.StringUtil;
import cn.cxion.chat.app.view.AddScheduleView;
import cn.cxion.chat.app.view.ChangePassWordView;
import cn.cxion.chat.app.view.CheckContractView;
import cn.cxion.chat.app.view.SettingView;

public class MyViewHolder extends RecyclerView.ViewHolder {
    @Bind(R.id.option)
    OptionItemView option;
    @Bind(R.id.ll_hold)
    LinearLayout llHold;
    Context context;
    private CheckContractBean checkContractBean;
    private CheckContractView checkContractView;
    private AddScheduleView addScheduleView;
    private AppointmentInfo appointmentInfo;

    public MyViewHolder(@NonNull View itemView, Context context) {
        super(itemView);
        this.context = context;
        ButterKnife.bind(this, itemView);
    }

    public AppointmentInfo getAppointmentInfo() {
        return appointmentInfo;
    }

    public void setAppointmentInfo(AppointmentInfo appointmentInfo) {
        this.appointmentInfo = appointmentInfo;
    }

    public CheckContractBean getCheckContractBean() {
        return checkContractBean;
    }

    public void setCheckContractBean(CheckContractBean checkContractBean) {
        this.checkContractBean = checkContractBean;
    }

    public void bindView(MyViewHolder holder, SecBean secBean) {
        viewControl(holder, secBean);
    }

    public void viewControl(MyViewHolder holder, SecBean secBean) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        switch (secBean.getClassId()) {
            case Constant.CHANGEBYOLDPWDVIEW:
            case Constant.CHANGEBYPHONEVIEW:
                llHold.addView(new ChangePassWordView(context, secBean.getClassId()).getView(), params);
                option.setVisibility(View.GONE);
                break;
            case Constant.CHECKCONTRACTVIEW:
            case Constant.CONTRACTCREATIONVIEW:
            case Constant.LEGALADVICEVIEW:
            case Constant.DEMANDDETAILS:
            case Constant.CLAIM:
            case Constant.CONTRACTDETAILS:
            case Constant.CLAIMDETAIL:
            case Constant.APPROVE:
            case Constant.CUSTOMERAPPROVE:
                if (checkContractBean == null)
                    checkContractView=new CheckContractView(context, secBean.getClassId());
                else
                    checkContractView=new CheckContractView(context, secBean.getClassId(),checkContractBean);
                llHold.addView(checkContractView.getView(), params);
                option.setVisibility(View.GONE);
                break;
            case Constant.NICKNAAME:
            case Constant.SEX:
            case Constant.CHANGEPHONE:
            case Constant.EMAIL:
            case Constant.POST:
            case Constant.ADDRESS:
                llHold.addView(new SettingView(context, secBean.getClassId()).getView(), params);
                option.setVisibility(View.GONE);
                break;

            case Constant.ADDSCHEDULE:
                if(appointmentInfo==null)
                    addScheduleView=new AddScheduleView(context,Constant.ADDSCHEDULE);
                else
                    addScheduleView=new AddScheduleView(context,Constant.ADDSCHEDULE,appointmentInfo);

                llHold.addView(addScheduleView.getView(), params);
                option.setVisibility(View.GONE);
                break;
            default:
                initConstant(holder, secBean);
                option.setVisibility(View.VISIBLE);
                break;
        }
    }


    public void initConstant(MyViewHolder holder, SecBean secBean) {
        holder.option.setLeftText(secBean.getOptionTitle());
        if (secBean.getLeftImage() != 0) {
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), secBean.getLeftImage());
            holder.option.setLeftImage(bitmap);
            holder.option.setLeftTextMarginLeft(LocalDisplay.dp2px(0));
        }
        if (!StringUtil.isEmpty(secBean.getRightText())) {
            holder.option.setRightText(secBean.getRightText());
        }
        holder.option.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intentControl(secBean.getClassId());
            }
        });
    }

    private void intentControl(int key) {
        switch (key) {
            case 0:
                break;
            case Constant.PASSWORD:
                ConstantActivity.show(context, Constant.SELECTMODE);
                break;
            default:
                ConstantActivity.show(context, key);
                break;
        }
    }
    public void rightClick(){
        if(checkContractView!=null){
            checkContractView.rightClick();
        }else if(addScheduleView!=null){
            addScheduleView.rightClick();
        }
    }
}